Plotting a Torus

To plot a surface in 3D do you first need to find the parametrization of the surface you wish to plot.

For a Torus the parametrization looks like this: \begin{align} x &= [c + a \cos v] \cos u \\ y &= [c + a \cos v] \sin u \\ z &= a \sin v \end{align}

where $a$ represents the radius of the tube and $c$ is the radius of the center hole of the torus tube. See https://mathworld.wolfram.com/Torus.html for further information.

Begin by activating matplotlib notebook and importing numpy and matplotlib


In [ ]:
%matplotlib notebook 
# This enables you to drag and rotate the figure

from mpl_toolkits.mplot3d import Axes3D # needed to do the 3D Plots
import numpy as np
import matplotlib.pyplot as plt

In [ ]:
fig = plt.figure()
ax = fig.gca(projection='3d')

u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, 2*np.pi, 100)
u, v = np.meshgrid(u, v) # Make coordinate matrices from coordinate vectors.
a, c = 0.2, 1.0 # Set a and c from the torus parametrization

# Paramatrization
x = (c + a*np.cos(v))*np.cos(u)
y = (c + a*np.cos(v))*np.sin(u)
z = a*np.sin(v)


ax.plot_surface(x, y, z)

ax.set_xlim3d(-1, 1)
ax.set_ylim3d(-1, 1)
ax.set_zlim3d(-1, 1)
plt.show()

Make a surface plot of a sphere

Plot a sphere the parametrization is:

\begin{align} x &= r \cos \theta \sin \phi \\ y &= r \sin \theta \sin \phi \\ z &= r \cos \phi \end{align}

where $\theta$ is the loggitude coorinate running from $0$ to $2\pi$, $\phi$ is the colatitude coordinate running from $0$ to $\pi$ and $r$ is the radius.


In [ ]:
fig = plt.figure()
ax = fig.gca(projection='3d')

# Fill inn the rest

Make a surface plot of a Mobius strip

The parametrization is: \begin{align} x &= \left(1 + \frac{v}{2}\cos \frac{u}{2} \right)\cos u \\ y &= \left(1 + \frac{v}{2}\cos v \right)\sin u \\ z &= \frac{v}{2}\sin \frac{u}{2} \end{align}

where $u$ is running from $0$ to $2\pi$ and $v$ is running from $-1$ to $1$.


In [ ]:
fig = plt.figure()
ax = fig.gca(projection='3d')

# Fill inn the rest

In [ ]: